package org.gp953.system.controller;

import io.swagger.annotations.ApiOperation;
import org.gp953.basic.util.AjaxResult;
import org.gp953.basic.util.PageList;
import org.gp953.system.domain.DictionaryDetail;
import org.gp953.system.query.DictionaryDetailQuery;
import org.gp953.system.service.IDictionaryDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/dicdetail")
public class DictionaryDetailController {
    @Autowired
    private IDictionaryDetailService dictionaryDetailService;

    /**
     * 通过id查询一个
     * @param id
     * @return DictionaryDetail
     */
    @GetMapping("/{id}")// {id}是占位符，@PathVariable表示从url的中拿值
    public DictionaryDetail queryById(@PathVariable("id") Long id){
        return dictionaryDetailService.queryById(id);
    }


    /**
     * 查询所有
     * @return
     */
    @GetMapping// {id}是占位符，@PathVariable表示从url的中拿值
    public List<DictionaryDetail> queryAll(){
        return dictionaryDetailService.queryAll();
    }

    /**
     * 添加和修改
     * @param dictionaryDetail
     * @return
     */
    @ApiOperation(value = "字典详情添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping // @RequestBody表示从请求体中拿值
    public AjaxResult addOrUpdate(@RequestBody DictionaryDetail dictionaryDetail){
        try {
            if (dictionaryDetail != null &&  dictionaryDetail.getId()!=null){
                dictionaryDetailService.update(dictionaryDetail);
            }else {
                dictionaryDetailService.add(dictionaryDetail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！！");
        }
    }

    /**
     *  通过id删除
     * @return
     * 定义接口的三要素：
     * 1.请求地址
     * 2.请求参数
     * 3.返回值
     */
    @DeleteMapping("/{id}")// {id}是占位符，@PathVariable表示从url的中拿值
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            dictionaryDetailService.delete(id);

            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败").setSuccess(false);
        }
    }


    /**
     * 高级查询
     * @param query
     * @return
     */
    @PostMapping// @RequestBody表示从请求体中拿值
    public PageList<DictionaryDetail> queryPage(@RequestBody DictionaryDetailQuery query){
        return dictionaryDetailService.queryPageData(query);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping("/dictionaryDetail")// @PatchMapping批量操作的注解
    // List<Long> ids 和Long[] ids 都可以接受数据，但在mapper映射文件中foreach标签要注意区分，使用list还是array
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            dictionaryDetailService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败").setSuccess(false);
        }
    }
    
}
